package cn.com.sqlite.demo.common.aspect;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson.JSON;

import cn.com.sqlite.demo.entity.po.log.SystemLogInfoPO;
import cn.com.sqlite.demo.service.SystemLogInfoService;

/**
 * @Description: AOP处理类
 * @author Y.Jin
 * @date 2017年2月7日 上午11:20:00
 */
@Aspect
@Component
public class SystemLogAspect {

	@Autowired
	private SystemLogInfoService service;

	/**
	 * 控制层切面点
	 */
	@Pointcut(value = "@annotation(cn.com.sqlite.demo.common.annotation.SystemControllerLog)")
	public void controllerAspect() {
	}

	/**
	 * 服务层控制层
	 */
	@Pointcut(value = "@annotation(cn.com.sqlite.demo.common.annotation.SystemServiceLog)")
	public void serviceAspect() {
	}

	/**
	 * 前置方法
	 * 
	 * @param joinPoint
	 * @throws Exception
	 */
	@Before(value = "controllerAspect()")
	public void doBefore(JoinPoint joinPoint) throws Exception {
		System.out.println("普通的请求日志");
		SystemLogInfoPO logInfoPO = new SystemLogInfoPO();
		logInfoPO.setCreateTime(new Date());
		logInfoPO.setExceptionInfo(" ");
		logInfoPO.setF1("待定");
		logInfoPO.setLogType(1);
		logInfoPO.setLogTypeDesc("普通日志");
		logInfoPO.setTargetClsName(joinPoint.getTarget().toString());
		logInfoPO.setCurrentMethod(joinPoint.getSignature().toString());
		logInfoPO.setParams(joinPoint.getKind());
		logInfoPO.setUserName("ADMIN");
		System.out.println(logInfoPO.toString());
		service.add(logInfoPO);
	}

	@AfterThrowing(pointcut = "serviceAspect()", throwing = "e")
	public void doAfterThrowing(JoinPoint joinPoint, Throwable e) throws Exception{
		System.out.println("异常信息扑捉日志");
		SystemLogInfoPO logInfoPO = new SystemLogInfoPO();
		logInfoPO.setCreateTime(new Date());
		logInfoPO.setExceptionInfo(e.toString());
		logInfoPO.setF1("待定");
		logInfoPO.setLogType(2);
		logInfoPO.setLogTypeDesc("异常信息");
		logInfoPO.setTargetClsName(joinPoint.getTarget().toString());
		logInfoPO.setCurrentMethod(joinPoint.getSignature().toString());
		logInfoPO.setParams(joinPoint.getKind());
		logInfoPO.setUserName("ADMIN");
		
		service.add(logInfoPO);
	}
}
